package com.yuanfeng.action.front.index.web;
import com.opensymphony.xwork2.ActionSupport;
import com.yuanfeng.action.front.index.service.IndexService;
import com.yuanfeng.pojo.Books;
import com.yuanfeng.pojo.Types;
import com.yuanfeng.util.PageBean;
import com.yuanfeng.util.WebUtil;
import org.apache.struts2.ServletActionContext;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
/**
 * 首页action
 * @author yuanfeng
 *
 */
public class IndexAction extends ActionSupport {
	/**
	 *这里必须使用接口接收，因为jdk的动态代理
	 */
	private IndexService indexServiceImpl;
	public void setIndexServiceImpl(IndexService indexServiceImpl) {
		this.indexServiceImpl = indexServiceImpl;
	}

	/**
	 * 主页
	 * @return
	 */
	public String  showIndex(){
		HttpServletRequest request = ServletActionContext.getRequest();
		//查询
		List<Types> types = indexServiceImpl.queryTypes();
		//放到request中去
		request.setAttribute("types",types);
		return "index";
	}

	/**
	 * 图书列表
	 * @return
	 */
	public String queryBooks(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String curPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		if(WebUtil.isEmpty(curPage)){
			curPage = "1";
		}
		if(WebUtil.isEmpty(pageSize)){
			pageSize = "8";
		}
		
		//先获取分类的id
		String typeId = request.getParameter("typeId");
		PageBean<Books> pageBean = indexServiceImpl.queryBooks(typeId, Integer.parseInt(curPage), Integer.parseInt(pageSize));
		//放到request中去
		request.setAttribute("pageBean", pageBean);
		//转发到一个页面上去显示
		return "booklist";
	}

	/**
	 * 图书明细
	 * @return
	 */
	public String queryBook(){
		HttpServletRequest request = ServletActionContext.getRequest();
		//获取书的id
		String bookId = request.getParameter("id");
		Books book = indexServiceImpl.queryBook(bookId);
		//将book放到request
		request.setAttribute("book",book);
		//转到一个明细页面上去显示
		return "bookdetail";
	}
	
}
